%infant filters loop - 1to3 months
framelist = readtable("C:/Users/erinm/OneDrive - Indiana University/MATLAB files/clusters_101122.csv");
framelist = framelist(framelist.ageweeks < 20,[1,8,9]);
framelist = unique(framelist,'rows');
framelist.image = framelist.fileNames;
framelist.folder(:) = framelist.subject;
%% 
impath = 'X:\1A_LooxcieBloomington\03_JPG_AllMasters';
mainPath = 'X:\1A_LooxcieBloomington\05_coding\13_VisualStatistics_2022\contrast and sf';
%cd(mainPath);
savePath = 'X:\1A_LooxcieBloomington\05_coding\13_VisualStatistics_2022\filtered_gauss';

numImages = height(framelist);

% set up variables to store before and after stats
lowSF_prop_0 = zeros(numImages,1);
contrast_sens_0 = zeros(numImages,1);
saturation_0 = zeros(numImages,1);
lowSF_prop_1 = zeros(numImages,1);
contrast_sens_1 = zeros(numImages,1);
saturation_1 = zeros(numImages,1);
%%
for fileCounter = 23420:numImages
    % Read input image
    input_image = imread(fullfile(impath,framelist.folder{fileCounter},framelist.image{fileCounter}));
    
    % sf lowpass filter based on cutoff -  butterworth filter
    %output_image = input_image;
    imageDims = size(input_image);
    imageDims = imageDims(1:2);
    
    output_image = imgaussfilt(input_image,2);
    % measure current level of contrast - need it to be BY SF
    im = double(output_image)/255;
    rms1 = sqrt(mean((im(:)-mean(im(:))).^2));

    % further reduce contrast by - 1-3
    if framelist.ageweeks(fileCounter) < 20
        contrast_factor = 0.4;
    else
        contrast_factor = 0.9;
    end
    
    av_lum = mean(im(:));
    scaled = ((im - av_lum)*contrast_factor) + av_lum;

    % if ageweeks < 20 - lower color intensity 
    hsv = rgb2hsv(scaled);
    % Desaturate.  Change this factor to vary the amount of desaturation.
    if framelist.ageweeks(fileCounter) < 20
        desaturationFactor = 0.75;
    else
        desaturationFactor = 1;
    end
    hsv(:, :, 2) = hsv(:, :, 2) * desaturationFactor;
    %hsv(:, :, 1) = hsv(:, :, 1) * desaturationFactor;
    % Convert back to RGB for display.
    rgb2 = hsv2rgb(hsv);

    output = rgb2;
    
    imwrite(output,fullfile(savePath,framelist.image{fileCounter}));
    if(mod(fileCounter,10)==0)
        disp(fileCounter)
    end
end

